<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="ruleName" label="档号规则名称">
        <el-input v-model="dataForm.ruleName" placeholder="请输入档号规则名称"></el-input>
      </el-form-item>
      <el-form-item prop="archiveId" label="所属全宗" class="role-list">
        <el-select v-model="dataForm.archiveId" placeholder="请选择所属全宗" @change="getBaseFieldList">
          <el-option v-for="generalArchive in generalArchiveList" :key="generalArchive.id" :label="generalArchive.name" :value="generalArchive.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="value" label="所属字段" class="role-list">
        <el-select v-model="dataForm.rule.value" placeholder="请选择所属全宗字段" ref="selectCh">
          <el-option v-for="field in baseFieldList" :key="field.id" :label="field.remark" :value="field.dictValue"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="valueStr" label="拼接字符串" class="role-list">
        <el-input v-model="dataForm.rule.valueStr" placeholder="拼接字符串"></el-input>
      </el-form-item>
      <el-form-item prop="sort" label="排序" class="role-list">
        <el-input type="number" v-model="dataForm.rule.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <el-card class="box-card">
      <p>{{ ruleStr }}</p>
    </el-card>
    <template v-slot:footer>
      <el-button @click="PJ()">点击拼接</el-button>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils"; // isEmail, isMobile, isNumber,
import { IObject } from "@/types/interface";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      generalArchiveName: "",
      generalArchiveList: [] as IObject[],
      baseFieldList: [] as any[],
      hidden: false,
      status: "",
      ruleStr: "",
      dataForm: {
        id: "",
        ruleName: "",
        archiveId: "",
        ruleList: [] as any,
        rule: {
          subEnd: "",
          subBegin: "",
          value: "",
          valueStr: "",
          sort: 0
        }
      }
    });
  },
  computed: {
    dataRule() {
      return {
        ruleName: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        archiveId: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.ruleStr =""
        this.dataForm.rule.valueStr =""
        this.$refs["dataFormRef"].resetFields();
        this.getGeneralArchiveList();
      });
    },
    getGeneralArchiveList() {
      baseService.get(`/archive/info/list`).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.generalArchiveList = res.data;
      });
    },
    getBaseFieldList(v) {
      baseService.get(`/archive/rule/field`, { id: v }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.baseFieldList = res.data;
      });
    },
    PJ() {
      this.$nextTick(() => {
        let data = {
          subEnd: "",
          subBegin: "",
          value: "",
          label: this.$refs.selectCh.selectedLabel,
          valueStr: "",
          sort: 0
        };
        data.subEnd = this.dataForm.rule.subEnd;
        data.subBegin = this.dataForm.rule.subBegin;
        data.value = this.dataForm.rule.value;
        let sort = this.dataForm.rule.sort;
        data.sort = sort;
        this.dataForm.ruleList.push(data);
        if (this.dataForm.rule.valueStr != null) {
          let dataStr = {
            subEnd: "",
            subBegin: "",
            value: "",
            label: "FIELD_JOINT",
            sort: sort + 1
          };
          dataStr.value = this.dataForm.rule.valueStr;
          this.dataForm.ruleList.push(dataStr);
        }
        this.ruleStr += data.label + this.dataForm.rule.valueStr;
        Object.keys(this.dataForm.rule)
          .filter((item) => item != "valueStr")
          .forEach((key) => (this.dataForm.rule[key] = ""));
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/archive/rule", {
          ...this.dataForm
        }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>

<style lang="less">
.mod-tenant__tenant {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}
</style>
